package com.unisound.tracelog.client.core.support.async;

import com.unisound.tracelog.client.core.context.SpanContext;
import com.unisound.tracelog.client.core.context.SpanContextManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class TraceLogRunnableWrapper implements Runnable {
    private static final Logger LOGGER = LoggerFactory.getLogger(TraceLogRunnableWrapper.class);

    private Runnable task;
    private List<SpanContext> spanContexts;

    public TraceLogRunnableWrapper(Runnable task) {
        this.task = task;
        this.spanContexts = SpanContextManager.getSpanContexts();
    }

    @Override
    public void run() {
        try {
            SpanContextManager.update(spanContexts);
        } catch (Exception e) {
            LOGGER.error("call error.", e);
        } finally {
            task.run();
        }
    }

}
